#include"Sort.h"

void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			++child;
		}

		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* a, int n)
{
	// 直接建堆
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
		AdjustDown(a, n, i);
	}

	// 升序建大堆
	int end = n - 1;
	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}


// hoare版本
void PartSort1(int* a, int left, int right)
{
	if (left >= right)
		return;

	int begin = left;
	int end = right;
	int keyi = left;

	while (left < right)
	{
		// 找小
		while (left < right && a[right] >= a[keyi])
		{
			--right;
		}

		//找大
		while (left < right && a[left] <= a[keyi])
		{
			++left;
		}

		// 各自找到，进行交换
		Swap(&a[left], &a[right]);
	}
	// 相遇
	Swap(&a[keyi], &a[left]);
	keyi = left;

	// [begin , keyi - 1] keyi [keyi + 1, end]
	PartSort1(a, begin, keyi - 1);
	PartSort1(a, keyi + 1, end);

}

// 前后指针法
void PartSort2(int* a, int left, int right)
{
	if (left >= right)
		return;

	int prev = left;
	int keyi = left;
	int cur = left + 1;

	// 单趟
	while (cur <= right)
	{
		if (a[cur] < a[keyi])
		{
			++prev;
			Swap(&a[prev], &a[cur]);
			++cur;
		}
		else
		{
			++cur;
		}
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;

	// [left, keyi - 1] keyi [keyi + 1, right]
	PartSort2(a, left, keyi - 1);
	PartSort2(a, keyi + 1, right);
}

#include"Stack.h"
// 非递归
void QuickSort(int* a, int left, int right)
{
	ST st;
	STInit(&st);
	STPush(&st, right);
	STPush(&st, left);

	while (!STEmpty(&st))
	{
		int begin = STTop(&st);
		STPop(&st);

		int end = STTop(&st);
		STPop(&st);

		// 单趟
		int prev = begin;
		int keyi = begin;
		int cur = begin + 1;

		while (cur <= right)
		{
			if (a[cur] < a[keyi])
			{
				++prev;
				Swap(&a[prev], &a[cur]);
				++cur;
			}
			else
			{
				++cur;
			}
		}
		Swap(&a[keyi], &a[prev]);
		keyi = prev;

		// [left, keyi - 1] keyi [keyi + 1, right]
		if (keyi + 1 < end)
		{
			STPush(&st, end);
			STPush(&st, keyi + 1);
		}

		if (begin < keyi - 1)
		{
			STPush(&st, keyi - 1);
			STPush(&st, begin);
		}
	}
	STDestroy(&st);
}

void _MergeSort(int* a, int begin, int end, int* temp)
{
	if (begin == end)
		return;

	int mid = (begin + end) / 2;

	// [begin, mid] [mid+1, end]
	_MergeSort(a, begin, mid, temp);
	_MergeSort(a, mid + 1, end, temp);

	// 归并
	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;

	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		// 把小的先尾插到temp数组中
		if (a[begin1] <= a[begin2])
		{
			temp[i++] = a[begin1++];
		}
		else
		{
			temp[i++] = a[begin2++];
		}
	}

	// 剩余的直接插入到temp数组中
	while (begin1 <= end1)
	{
		temp[i++] = a[begin1++];
	}

	while (begin2 <= end2)
	{
		temp[i++] = a[begin2++];
	}
	// 把数据拷贝到原数组里
	// 每次递归的时候a, temp 的位置可能不同，所以要加上begin 
	memcpy(a + begin, temp + begin, sizeof(int) * (end - begin + 1));
}

// 归并 -- 递归
void MergeSort1(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	if (temp == NULL)
	{
		perror("malloc fail");
		return;
	}

	_MergeSort(a, 0, n - 1, temp);

	free(temp);
	temp = NULL;
}

// 归并 --- 非递归
void MergeSort(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	if (temp == NULL)
	{
		perror("malloc fail");
		return;
	}

	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap) // 成gap倍进行排序
		{
			int begin1 = i, end1 = begin1 + gap - 1;
			int begin2 = begin1 + gap, end2 = begin2 + gap - 1;

			// 越界的问题处理
			if (end1 >= n || begin2 >= n)
				break;

			if (end2 >= n)
				end2 = n - 1;

			int j = i;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] <= a[begin2])
				{
					temp[j++] = a[begin1++];
				}
				else
				{
					temp[j++] = a[begin2++];
				}
			}

			while (begin1 <= end1)
			{
				temp[j++] = a[begin1++];
			}

			while (begin2 <= end2)
			{
				temp[j++] = a[begin2++];
			}
			memcpy(a + i, temp + i, sizeof(int) * (end2 - i + 1));
		}
		gap *= 2;
	}
	free(temp);
	temp = NULL;
}

